package com.ruoyi.news.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.news.domain.NewsComment;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.news.domain.News;
import com.ruoyi.news.service.INewsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 新闻管理Controller
 * 
 * @author 祁正
 * @date 2023-06-03
 */
@RestController
@RequestMapping("/news/news")
public class NewsController extends BaseController
{
    @Autowired
    private INewsService newsService;

    /**
     * 新增新闻评论
     */
    @PreAuthorize("@ss.hasPermi('news:news:list')")
    @PostMapping("/addComment")
    public AjaxResult addComment(@RequestBody NewsComment newsComment){
        int res = newsService.addNewsComment(newsComment);
        return AjaxResult.success(res>0);
    }

    /**
     *
     * 新闻点赞
     */

    @PreAuthorize("@ss.hasPermi('news:news:list')")
    @GetMapping("/addLikeNumber/{lid}")
    public AjaxResult addLikeNumber(@PathVariable("lid") int lid){
        int i = newsService.addLikeNumber(lid);
        return AjaxResult.success(i>0);
    }


    /**
     * 根据新闻lid获取新闻评论
     */
    @PreAuthorize("@ss.hasPermi('news:news:list')")
    @GetMapping("/comment/{lid}")
    public AjaxResult getComments(@PathVariable int lid) {
        List<NewsComment> comments = newsService.getComments(lid);
        return AjaxResult.success(comments);
    }

    /**
     * 根据新闻id获取新闻详细信息
     *
     */
    @PreAuthorize("@ss.hasPermi('news:news:list')")
    @GetMapping("/detail/{lid}")
    public AjaxResult selectNewsByLid(@PathVariable int lid){

        return AjaxResult.success(newsService.gettNewsById(lid));
    }

    /**
     * 新闻的浏览量加1
     */
    @PreAuthorize("@ss.hasPermi('news:news:list')")
    @PutMapping("/addReadNumber/{lid}")
    public AjaxResult addReadNumber(@PathVariable int lid){
        int res = newsService.newsReadNumAdded(lid);
        return AjaxResult.success(res>0);
    }

    /**
     * 查询新闻管理列表
     */
    @PreAuthorize("@ss.hasPermi('news:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(News news)
    {
        startPage();
        List<News> list = newsService.selectNewsList(news);
        return getDataTable(list);
    }

    /**
     * 导出新闻管理列表
     */
    @PreAuthorize("@ss.hasPermi('news:news:export')")
    @Log(title = "新闻管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, News news)
    {
        List<News> list = newsService.selectNewsList(news);
        ExcelUtil<News> util = new ExcelUtil<News>(News.class);
        util.exportExcel(response, list, "新闻管理数据");
    }

    /**
     * 获取新闻管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('news:news:query')")
    @GetMapping(value = "/{lid}")
    public AjaxResult getInfo(@PathVariable("lid") Long lid)
    {
        return success(newsService.selectNewsByLid(lid));
    }

    /**
     * 新增新闻管理
     */
    @PreAuthorize("@ss.hasPermi('news:news:add')")
    @Log(title = "新闻管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody News news)
    {
        return toAjax(newsService.insertNews(news));
    }

    /**
     * 修改新闻管理
     */
    @PreAuthorize("@ss.hasPermi('news:news:edit')")
    @Log(title = "新闻管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody News news)
    {
        return toAjax(newsService.updateNews(news));
    }

    /**
     * 删除新闻管理
     */
    @PreAuthorize("@ss.hasPermi('news:news:remove')")
    @Log(title = "新闻管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{lids}")
    public AjaxResult remove(@PathVariable Long[] lids)
    {
        return toAjax(newsService.deleteNewsByLids(lids));
    }
}
